<?php
	include "include/common.inc.php";

	$user = getLoginUser();
	
	$start=isset($_GET["start"])?(int)$_GET["start"]:0;
	$order=isset($_GET["order"])?$_GET["order"]:"`id` DESC";

	function showPost($post){
		$group = Group::getGroup($post->getValue("groupId"));
		$author = User::getUser($post->getValue("authorId"));
		echo "<tr>";
		echo "<td><a href=\"post.php?postId=".$post->getValue("id")."\">".$post->getValue("title")."</a></td>";
		echo "<th><a href=\"group.php?groupId=".$post->getValue("groupId")."\">".$group->getValue("name")."</a></th>";
		echo "<td>By <a href=\"user.php?userId=".$post->getValue("authorId")."\">".$author->getValue("userName")."</a><br>".$post->getValue("timestamp")."</td>";
		echo "</tr>";
	}
	
	function DisplayFirstEightGroup(){
	$user=new User(array("id" => -1));
	//$tag="Group:";
	$image= array();
	for($i=0;$i<8;$i++)
	{
	$image[$i]="first".(string)$i.".jpg";
	//echo "<p>{$image[$i]}</p>";
	}
	list($groups,$totalRows)=Group::getGroups($startRow = 0, $numRows = PAGE_SIZE, $order="", $name="");
	print"<div class=\"row\">";
	//print(PAGE_SIZE);
	if($totalRows>8) {$totalRows=8;}
	for($i=0;$i<$totalRows;$i++)
	{
	if (($i % 4 ) == 0) 
	{
	print"</div>";
	print"<div class=\"row\">";
	}
			showGroup($groups[$i],$user,$image[$i]);
	}
			print"</div>";
					
	}
	
	
	function DisplayPopularGroup($user){
	//$tag="Popular Group:";
	$image= array();
	for($i=0;$i<10;$i++)
	{
	$image[$i]="pop".(string)$i.".jpg";
	//echo ($image[$i]);
	}
	list($groups,$totalRows)=Group::getHotGroups($startRow = 0, $numRows = PAGE_SIZE);
	//echo ( $totalRows);
	//echo($totalRows);
	print"<div class=\"row\">";
	if($totalRows>10) {$totalRows=10;}
	for($i=0;$i<$totalRows;$i++)
	{
	if (($i % 4 ) == 0) 
	{
	print"</div>";
	print"<div class=\"row\">";
	}
			showGroup($groups[$i],$user,$image[$i]);
	}
			print"</div>";
					
	}
	
	function DisplayUserGroups($user){
	//$tag="Your Joined Group:";
	$userId= $user->getValue("id");
$image=array();
	for($i=0;$i<8;$i++)
	{
	$image[$i]="join".(string)$i.".jpg";
	//echo ($image[$i]);
	}
	
	
	list($groups,$totalRows)=$user->getJoinedGroups($startRow = 0, $numRows = PAGE_SIZE);
	print"<div class=\"row\">";
	$k=0;
	for($i=0;$i<$totalRows;$i++)
	{
	if($k>=20)
	$k=$k%8;
	
	if (($i % 4 ) == 0) 
	{
	print"</div>";
	print"<div class=\"row\">";
	}
			showGroup($groups[$i],$user,$image[$k++]);
	}
			print"</div>";
}

	function showGroup($g,$user,$image){
	
	echo"<div class=\"col-sm-6 col-md-3\"  >";
    echo"<div class=\"thumbnail\">";
     echo" <img src=images/{$image} alt=\"{$g->getValue("name")}\"  style=\"height:200px;\" >";
      echo"<div class=\"caption\" style=\"height:200px;\">";
       echo" <h3>{$g->getValue("name")}</h3>";
       echo" <p>{$g->getValue("description")}</p>";
         if($user->getValue("id") == -1)
		 {
        echo "<p><a href=\"Login.php\" class=\"btn btn-primary\" role=\"button\">Post</a></p>";
		}
		else 
		{
		
		echo"<p><a href=\"group.php?groupId={$g->getValue("id")}\" class=\"btn btn-primary\" role=\"button\">Post</a></p>";
		}
      echo"</div>";
    echo"</div>";
  echo"</div>";
	
	}
	
	

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Blind Dating</title>
	<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
    <!-- Bootstrap -->
    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	<script type= "text/javascript" src = "js/countries2.js"></script>
  </head>
  <body>
  
  <nav class="navbar navbar-default " role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header" >
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="./index.php">NUS Blind Dating Site</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="./index.php">Home</a></li>
        <li><a href="About.php">About</a></li>
        <li><a href="groups.php">Groups</a></li>
	
		
      
      </ul>
      <form class="navbar-form navbar-right" role="search" action="search_group.php" method="get">
        <div class="form-group">
          <input name ="search" type="text" class="form-control" placeholder="Search Group">
        </div>
        <button type="submit" class="btn btn-default" name="submit" value="submit">Search</button>
      </form>
	  <ul class="nav navbar-nav navbar-right">
	  <?php if ($user->getValue("id")==-1) {?>
        <li><a href="Register.php">Register</a></li>
        <li><a href="Login.php">Login</a></li>
      
	  <?php }else{ ?>
	  
	   <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello! <?php echo $user->getValue("userName"); ?> <b class="caret"></b></a>
          <ul class="dropdown-menu">
		  <li><a href="Myprofile.php">View My Profile</a></li>
		<li><a href="Editprofile.php">Edit My Profile</a></li>
        <li><a href="Groups.php">SeeGroup</a></li>
		<li><a href="Logout.php">Logout</a></li>
          </ul>
		  
        </li>
	  <?php }?>
	  </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
	
	

<!--right side is a easy registration-->

<div id= "rightside" style="float:right; width:25%; background-image:url(images/pink.jpg);">
<style>
table, th, td
{
border:none;
padding:9px;
}

</style>
<?php if ($user->getValue("id")==-1) {?>
<h2 align="center"class="form-signin-heading">Quick Register!</h2>
<form action="Register.php" method ="post" name="register",class="form-signin" role="form" >
    <table>
	<tr>
        <td><label>FirstName</label></td>
        <td><input type="text" class="form-control" placeholder="FirstName" required="" autofocus name="firstname"></td>
      </tr>
	  <tr>
        <td><label>LastName</label></td>
        <td><input type="text" class="form-control" placeholder="LastName" required="" autofocus name="lastname"></td>
      </tr>
	<tr>
        <td><label>UserName</label></td>
        <td><input type="text" class="form-control" placeholder="UserName" required="" autofocus name="username"></td>
      </tr>
	  <tr>
        <td><label>Gender</label></td>
        <td>
		<select name="gender" class="form-control" required autofocus>
		<option value="Male"> Male</option>
		<option value="Female"> Female</option>
		</select>
		</td>
      </tr>
      <tr>
        <td><label>Email</label></td>
        <td><input type="email" class="form-control" placeholder="Email address" required="" autofocus name="email"></td>
      </tr>
	  
	 <tr>
        <td><label>Nation</label></td>
        <td><select class="form-control"  onChange="print_state('state',this.value);" id="country" name = "country"></select></td>
      </tr>
	  <tr>
	  <td><label>State</label></td>
	  <td><select class="form-control" name ="state" id = "state" ></select>
<script language="javascript">print_country("country","Singapore");</script>
<script language="javascript">print_state("state","Singapore","Singapore");</script>
</td>
	  </tr>
	  
	  <tr>
	  <td><label> Birthday</label> </td>
	  <td><input type="date" class="form-control" placeholder="Birthday"  autofocus="" name="birthday"></td>
	  </tr>
	  
      <tr>
	    <td><label>Password</label></td>
        <td><input type="password" class="form-control" placeholder="Password" required="" name="password"></td>
      </tr>
	  <tr>
	    <td><label>Confirmed Password</label></td>
        <td><input type="password" class="form-control" placeholder="Confirmed Password" required="" name="CPassword" ></td>
      </tr>

	  <tr>
      <td width= "50%" align= "left"> <button class="btn btn-lg btn-primary btn-block" type="submit" name="Register" value="Register">Register</button></td>
      <td width ="50%" align="right"> <button class="btn btn-lg btn-primary btn-block" type="submit" name="Reset" value="Reset">Reset</button></td>
	  <tr>
    </table>
  </form>
  <?php }else{?>
<table>
<h2 align="center"><?php echo $user->getValue("userName");?> 's Profile</h2>
  <tr>
    <th scope="row" width="40%" align="left" >Type</th>
    <td width="60%"><?php  if($user->getValue("type") == "1") echo "Admin"; else echo "Normal User";  ?></td>
    </tr>
  <tr>
  <tr>
    <th scope="row" width="40%" align="left" >First Name</th>
    <td width="60%"><?php echo $user->getValue("firstName"); ?></td>
    </tr>
  <tr>
    <th scope="row" align="left">Last Name</th>
    <td><?php echo $user->getValue("lastName"); ?></td>
    </tr>
  <tr>
    <th scope="row" width="40%" align="left" >Email</th>
    <td width="60%"><?php  echo $user->getValue("email")  ?></td>
  </tr>
  <tr>
  <tr>
    <th scope="row" align="left">Gender</th>
    <td><?php if($user->getValue("gender") == "Male") echo "Male"; else echo "Female"; ?></td>
    </tr>
  <tr>
    <th scope="row" align="left">Birthday</th>
    <td><?php echo extractDate($user->getValue("birthday")); ?></td>
    </tr>
  <tr>
    <th scope="row" align="left">Nation</th>
    <?php $city = City::getCityById($user->getValue("cityId")); ?>
    <td><?php echo $city->getValue("country"); ?></td>
    </tr>
   <tr>
    <th scope="row" align="left">City/District/State</th>
    <td><?php echo $city->getValue("name"); ?></td>
    </tr>
</table>
  <?php }?>
</div>

<!-- Group the jumbo and successive pictures of users-->
	<div style="float:left; width:72%;">
 <div class="jumbotron">
 <?php if($user->getValue("id") == -1) {?>
  <h1>Welcome to NUS Blind Dating Site</h1>
  <?php }else{?>
  <h1>Welcome! <?php echo $user->getValue("userName"); ?></h1>
  <?php }?>
  <p>This is a professional site for guys and girls to find their perfect dates.</p>
  <p><a class="btn btn-primary btn-lg" role="button" href="About.php">Learn more</a></p>
</div>

<!--following are pictures for girls and guys-->
<?php if($user->getValue("id")!=-1) {?>
<h3>Your Joined Groups: </h3>
<?php DisplayUserGroups($user); ?>

<?php  } else {?> 
<h3>The First 8 Groups on Site:</h3>
<?php  DisplayFirstEightGroup();} ?>

<h3>Top 10 Popular: </h3>
<?php DisplayPopularGroup($user);?> 
</div>




    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
	
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

